//
//  profileDetailViewController.swift
//  AreaChainblock
//
//  Created by 赵佟越 on 2018/9/18.
//  Copyright © 2018年 zhaotongyue. All rights reserved.
//

import UIKit
import Eureka
import SwiftyUserDefaults
//1.
typealias swiftBlock = (_ str: String) -> Void


class profileDetailViewController: FormViewController {
    var changeStr : NSString! = ""
    var callBack: swiftBlock?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        layoutNav()
        makeBaseUI()
        // Do any additional setup after loading the view.
    }
    func makeBaseUI() {
        super.tableView.frame = CGRect(x: 15, y: 64, width: kScreenWidth - 30, height: kScreenHeight - 64)
        if #available(iOS 11.0.0, *) {
            super.tableView.frame = CGRect(x: 0, y: 0, width: kScreenW, height: kScreenHeight - 0)
        }
        let backImageView = UIImageView.init()
        backImageView.frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight)
        backImageView.image = UIImage.init(named: "底色 拷贝 2@2x")
        self.view.addSubview(backImageView)
        self.view.sendSubview(toBack: backImageView)
        
        LabelRow.defaultCellUpdate = { cell, row in
            cell.textLabel?.textColor = .white
            cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 15)
            cell.textLabel?.textAlignment = .right
        }
        PushRow<String>.defaultCellUpdate = { cell, row in
            cell.textLabel?.textColor = .white
            cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 15)
            cell.textLabel?.textAlignment = .right
        }
        TextRow.defaultCellUpdate = { cell, row in
            cell.textLabel?.textColor = .white
            cell.backgroundColor = UIColor.clear
            cell.detailTextLabel?.textColor = UIColor.white
            cell.textField?.textColor = UIColor.white
            cell.titleLabel?.textColor = .white
            cell.textLabel?.font = UIFont.boldSystemFont(ofSize: 15)
            cell.textLabel?.textAlignment = .right
        }
        super.tableView.backgroundColor = UIColor.clear
        
        form +++ Section("")
            //为区域添加一个标签行
            
            <<< TextRow(XALanguageManager.getString(key: "昵称")){
                $0.title = $0.tag
                $0.value = String(changeStr)
                }//选中标签行会在tittle后面加上" Summer "
                .onChange({ (textField) in
                    if textField.value == nil{
                        self.changeStr = ""
                    }else{
                        self.changeStr = textField.value! as NSString
                    }
                    
                })
    }
    
    func layoutNav(){
        self.title = XALanguageManager.getString(key: "修改")
        let btn1 = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 44));
        btn1.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        btn1.setTitle(XALanguageManager.getString(key: "保存"), for: .normal)
        btn1.addTarget(self, action: #selector(profileDetailViewController.changeAction), for: .touchUpInside)
        btn1.setTitleColor(UIColor.white, for: .normal)
        let item2 = UIBarButtonItem(customView: btn1);        self.navigationItem.rightBarButtonItem = item2
    }
//3.
    func callBackBlock(_ block: @escaping swiftBlock) {
        
        callBack = block
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }

    @objc func changeAction(){
        
        MyNetwork.request(target: .updateUserinfo(user_id: Defaults[.userPhone]!, nick_name: changeStr as String, headImg: "", check_type:""), success: { (result) in
                kLog(result)
                let needModel = BaseModel()
                needModel.yy_modelSet(with: result as! [AnyHashable : Any])
                
                if needModel.code == 1111{
//                    let userModel = LogInModel()
//                    postValueBlock!("传值到上一页");
                    if self.callBack != nil {
                        
                        self.callBack!(self.changeStr as String)
                        
                    };
                    self.navigationController?.popToRootViewController(animated: true)
                }else{
                    self.navigationController?.view.makeToast(XALanguageManager.getString(key: "失败"))
                }
                
            }) { (error) in
                //kLog(error.localizedDescription)
                self.navigationController?.view.makeToast(error.localizedDescription)
            }
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
